/*
 * Copyright 2010 CSsula.twgg.org. All Rights Reserved.
 */

package csrpg;

import org.twgg.cssula.ConsoleTool;
import java.io.*;
import java.util.*;
import java.net.*;

public class Main {
    public static void main(String[] args) throws Exception{
        new Game();
    }
}

class Game{
    public static final Object lock = new Object();
    public static ConsoleTool ct = new ConsoleTool();
    public static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    public static Random rand = new Random();
    public static Map map;
    public static Screen screen;
    private Role player;
    private Socket srvSock;
    private DataInputStream sockIn;
    private DataOutputStream sockOut;

    public Game(){
        init();
        login();
        ConsoleTool.cls();
        drawWindow(0, 0, 21, 21, ConsoleTool.YELLOW | ConsoleTool.BRIGHT);
        screen.start();
        
        map.add(player, 2, 2);
        synchronized(lock){
            Role role;
            for(int i=0;i<25;i++){
                role = new NPC(map,(char)('A'+i%26), ConsoleTool.YELLOW |ConsoleTool.BRIGHT, rand.nextInt(401)+100);
                map.add(role, rand.nextInt(map.cols-4)+2, rand.nextInt(map.rows-4)+2);
                map.delete(role);
            }
        //for(int i=0;i<25;i++) map.delete(String.valueOf((char)('A'+i%26)));
        }
        
        screen.focus(player);
    }

    private void init(){
        try{
            srvSock = new Socket("localhost", 1123);
            sockIn = new DataInputStream(new BufferedInputStream(srvSock.getInputStream()));
            sockOut = new DataOutputStream(new BufferedOutputStream(srvSock.getOutputStream()));
        }catch(Exception e){
            ct.cls();
            e.printStackTrace();
        }
        ConsoleTool.cls();
        ConsoleTool.setTitle("兜神工作室 - CSRPG");
        ct.setCursorVisible(false);
        ct.setWindowSize(23, 24);
        map = new Map("map/map");
        screen = new Screen(map);
    }

    private void login(){
        drawWindow(2, 4, 3, 16, ConsoleTool.YELLOW | ConsoleTool.BRIGHT);
        String s;
        for(;;){
            while((s = questionField(4, 6, "請輸入能夠代表您的一個字：", 1)).length()!=1);
            try{
                sockOut.writeChar(s.charAt(0));
                sockOut.flush();
                if(sockIn.readBoolean()==true) break;
                ct.gotoXY(4, 9);
                ct.setColor(ConsoleTool.WHITE);
                System.out.print("這個名字已被註冊");
            }catch(Exception e){
                ct.cls();
                e.printStackTrace();
            }
        }
        player = new Player(map, s.charAt(0), ConsoleTool.INDIGO | ConsoleTool.BRIGHT,100);
    }

    public String questionField(int x, int y, String question, int length){
        String ans = null;
        ct.gotoXY(x, y);
        ct.setColor(ConsoleTool.WHITE);
        System.out.print(question);
        return drawTextField(x+(question.getBytes().length-1)/2+1, y, length);
    }

    public String drawTextField(int x, int y, int length){
        byte[] tmp = new byte[length*2+1];
        int p=0;
        ct.setColor(ConsoleTool.WHITE_BACKGROUND | ConsoleTool.BRIGHT_BACKGROUND);
        ct.gotoXY(x, y);
        for(int i=0;i<length*2;i++) System.out.print(' ');
        ct.gotoXY(x, y);
        ct.setCursorVisible(true);
        while(true){
            byte b = (byte)ConsoleTool.getCh();
            if(b==ConsoleTool.VK_RETURN){
                break;
            }else if(b==ConsoleTool.VK_BACK){
                 if(p>0) tmp[--p] = '\0';
            }else if(p<length*2){
                tmp[p++] = b;
            }
            ct.gotoXY(x, y);
            for(int i=0;i<length*2;i++) System.out.print(' ');
            ct.gotoXY(x, y);
            System.out.print(new String(tmp).trim());
            System.out.flush();
        }
        System.gc();
        ct.setCursorVisible(false);
        ct.setColor(ConsoleTool.BLACK);
        return new String(tmp).trim();
    }

    public static void drawWindow(int x, int y, int rows, int cols, int color){
        ct.setColor(color);
        for (int i = x + 1; i < cols + x + 1; i++) {
            ct.gotoXY(i, y);
            System.out.print("▁");
            ct.gotoXY(i, rows + y + 1);
            System.out.print("▔");
        }
        for (int i = y + 1; i < rows + y + 1; i++) {
            ct.gotoXY(x, i);
            System.out.print("▕");
            ct.gotoXY(cols + x + 1, i);
            System.out.print("▏");
        }
    }
}

